


PM_FUNC_NORM_ATTRIB FileName( PM_FILES:iFileNum )
{
	static String:szPokeDir[128];
	if ( !szPokeDir[0] )
	{
		get_configsdir( szPokeDir, charsmax(szPokeDir) );
		add( szPokeDir, charsmax(szPokeDir), "/pokemod/" );
		if ( !dir_exists( szPokeDir ) )
			mkdir( szPokeDir );
	}

	static String:szFileName[128];
	copy( szFileName, charsmax(szFileName), szPokeDir );

	switch( iFileNum )
	{
		case FILE_ITEMS:        add( szFileName, charsmax(szFileName), "items.ini" );
		case FILE_SKILLS:       add( szFileName, charsmax(szFileName), "skills.ini" );
		case FILE_CONFIG:       add( szFileName, charsmax(szFileName), "pokemod.cfg" );
		case FILE_POKEMON:      add( szFileName, charsmax(szFileName), "pokemon.ini" );
		case FILE_SETTINGS:     add( szFileName, charsmax(szFileName), "settings.ini" );
		case FILE_FUNCTIONS:    add( szFileName, charsmax(szFileName), "functions.ini" );
		case FILE_LOCATIONS:    add( szFileName, charsmax(szFileName), "locations.ini" );
	}

	return szFileName;
}

PM_FUNC_NORM_ATTRIB FileSeekToName( Int:iFileHandle, String:szName[], Int:iAssumedTell=0, &Int:iTell=0, Int:cPrefix='#' )
{
	pokemod_funcstart_debug( "FileSeekToName" );

	new String:szData[128], bool:bFound = false;

	if ( iAssumedTell )
	{
		fseek( iFileHandle, iAssumedTell, SEEK_SET );

		fgets( iFileHandle, szData, charsmax(szData) );
		trim( szData );

		if ( szData[0] == cPrefix && equal( szData[1], szName ) )
		{
			iTell = iAssumedTell;
			bFound = true;
		}
		else
		{
			fseek( iFileHandle, 0, SEEK_SET );
		}
	}

	if ( !bFound )
	{
		while ( !feof( iFileHandle ) )
		{
			iTell = ftell( iFileHandle );

			fgets( iFileHandle, szData, charsmax(szData) );
			trim( szData );

			if ( szData[0] == cPrefix && equal( szData[1], szName ) )
			{
				bFound = true;
				break;
			}
		}
	}

	pokemod_funcend_debug( "FileSeekToName" );

	return bFound;
}

PM_FUNC_NORM_ATTRIB FileReadPokemon()
{
	pokemod_funcstart_debug( "FileReadPokemon" );

	SetupArray( g_iPokemonNames, NAME_LEN );
	SetupArray( g_iPokemonTells, 1 );
	SetupTrie( g_iPokemonTrie );

	ArrayPushString( g_iPokemonNames, "None" );
	ArrayPushCell( g_iPokemonTells, 0 );

	new String:szData[128], Int:iTell;
	new Int:iFileHandle = fopen( FileName( FILE_POKEMON ), "rt" );
	while ( !feof( iFileHandle ) )
	{
		iTell = ftell( iFileHandle );
		fgets( iFileHandle, szData, charsmax(szData) );
		trim( szData );

		if ( szData[ 0 ] == ';' || ( szData[ 0 ] == '/' && szData[ 1 ] == '/' ) )
			continue;

		if ( szData[0] == '#' )
		{
			TrieSetCell( g_iPokemonTrie, szData[1], ArraySize( g_iPokemonNames ) );
			ArrayPushString( g_iPokemonNames, szData[1] );
			ArrayPushCell( g_iPokemonTells, iTell );
		}
	}
	fclose( iFileHandle );

	pokemod_funcend_debug( "FileReadPokemon %d", ArraySize( g_iPokemonNames ) - 1 );
}

PM_FUNC_NORM_ATTRIB FileReadItems()
{
	pokemod_funcstart_debug( "FileReadItems" );

	SetupArray( g_iItemNames, NAME_LEN );
	SetupArray( g_iItemTells, 1 );
	SetupArray( g_iItemProps, 1 );
	SetupTrie( g_iItemTrie );

	new String:szData[128], Int:iTell, Int:iProp, ITEM_PROP:iPropNum;
	new Int:iFileHandle = fopen( FileName( FILE_ITEMS ), "rt" );
	while ( !feof( iFileHandle ) )
	{
		iTell = ftell( iFileHandle );
		fgets( iFileHandle, szData, charsmax(szData) );
		trim( szData );

		if ( szData[ 0 ] == ';' || ( szData[ 0 ] == '/' && szData[ 1 ] == '/' ) )
			continue;

		if ( szData[ 0 ] == '#' )
		{
			TrieSetCell( g_iItemTrie, szData[1], ArraySize( g_iItemNames ) );
			ArrayPushString( g_iItemNames, szData[1] );
			ArrayPushCell( g_iItemTells, iTell );
			ArrayPushCell( g_iItemProps, 0 );
		}
		else if ( equali( szData, "Prop ", 5 ) )
		{
			//Remove Prop
			copy( szData, charsmax(szData), szData[ 5 ] );

			iProp = 0;
			new String:szLeft[ 64 ];
			while ( ValidData( szData ) )
			{
				strbreak( szData, szLeft, charsmax(szLeft), szData, charsmax(szData) );
				for ( iPropNum = ITEM_DEFAULT; iPropNum < ITEM_PROP; iPropNum++ )
				{
					if ( equali( szLeft, szItemProp[ iPropNum ] ) )
					{
						iProp |= ( 1 << _:iPropNum );
						break;
					}
				}
				if ( iPropNum == ITEM_PROP )
				{
					pokemod_debug( "FileReadItems Unknown Item Prop: '%s'", szLeft );
				}
			}
			ArraySetCell( g_iItemProps, ArraySize( g_iItemProps ) - 1, iProp );
		}
	}
	fclose( iFileHandle );

	pokemod_funcend_debug( "FileReadItems %d", ArraySize( g_iItemNames ) );
}

PM_FUNC_NORM_ATTRIB FileReadLocations()
{
	pokemod_funcstart_debug( "FileReadLocations" );

	SetupArray( g_iLocationNames, NAME_LEN );
	SetupArray( g_iLocationTells, 1 );
	SetupTrie( g_iLocationTrie );

	new String:szData[128], Int:iTell;
	new Int:iFileHandle = fopen( FileName( FILE_LOCATIONS ), "rt" );
	while ( !feof( iFileHandle ) )
	{
		iTell = ftell( iFileHandle );
		fgets( iFileHandle, szData, charsmax(szData) );
		trim( szData );

		if ( szData[ 0 ] == ';' || ( szData[ 0 ] == '/' && szData[ 1 ] == '/' ) )
			continue;

		if ( szData[0] == '#' )
		{
			if ( equali( szData[1], "Global" ) )
				g_iLocationGlobal = ArraySize( g_iLocationNames );

			TrieSetCell( g_iLocationTrie, szData[1], ArraySize( g_iLocationNames ) );
			ArrayPushString( g_iLocationNames, szData[1] );
			ArrayPushCell( g_iLocationTells, iTell );
		}
	}
	fclose( iFileHandle );

	pokemod_funcend_debug( "FileReadLocations %d", ArraySize( g_iLocationNames ) );
}

PM_FUNC_NORM_ATTRIB FileScript( Int:id, String:szFileName[], String:szName[], String:szForward[], String:szArgument[], Int:iAssumedTell=0, &Int:iTell=0, bool:plus = false )
{
	new Int:iFileHandle = fopen( szFileName, "rt" );
	if ( !FileSeekToName( iFileHandle, szName, iAssumedTell, iTell ) )
		return;

	new String:szData[ 128 ], String:szForwardName[ 64 ];
	while ( !feof( iFileHandle ) )
	{
		fgets( iFileHandle, szData, charsmax(szData) );
		trim( szData );

		if ( szData[0] == '#' )
			break;

		if ( szData[ 0 ] == ';' || ( szData[ 0 ] == '/' && szData[ 1 ] == '/' ) )
			continue;

		ScriptLineForward( szData, szForwardName, charsmax(szForwardName), szData, charsmax(szData) );

		if ( ScriptCompareForward( szForwardName, szForward, szArgument, plus ) )
		{
			if ( contain( szData, "%arg" ) != -1 )
			{
				replace_all( szData, charsmax(szData), "%arg%", szArgument );
				replace_all( szData, charsmax(szData), "%arg1%", szArgument );
			}

//			pokemod_debug( "ScriptCompareForward %s %s %s", szForwardName, szForward, szArgument );
			ScriptDoLine( id, szData );
		}
	}

	fclose( iFileHandle );
}




